package com.yusheng.tr.multi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yusheng.exceptions.ServiceException;
import com.yusheng.tr.common.core.entity.multi.ModelMngEntity;
import com.yusheng.tr.common.core.model.dto.multi.ModelMngDto;
import com.yusheng.tr.multi.mapper.ModelMngMapper;
import com.yusheng.tr.multi.service.IModelMngService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 模型管理表 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-10-02
 */
@Service
public class ModelMngServiceImpl extends ServiceImpl<ModelMngMapper, ModelMngEntity> implements IModelMngService {

    @Override
    @Transactional
    public void addOrUpdate(ModelMngDto dto) {
        ModelMngEntity modelMngEntity;
        if (CollectionUtils.isEmpty(dto.getDetailDtoList())){
            modelMngEntity = getOne(new LambdaQueryWrapper<ModelMngEntity>().eq(ModelMngEntity::getName,dto.getName()));
            if (modelMngEntity != null){
                throw new ServiceException("模型名称已存在");
            }
        }
        modelMngEntity = new ModelMngEntity();
        BeanUtils.copyProperties(dto,modelMngEntity);
        saveOrUpdate(modelMngEntity);
        String id = modelMngEntity.getId();
        if (!CollectionUtils.isEmpty(dto.getDetailDtoList())){
            List<ModelMngEntity> mngEntities = new ArrayList<>();
            dto.getDetailDtoList().forEach(dl ->{
                ModelMngEntity entity = new ModelMngEntity();
                entity.setId(dl.getId());
                entity.setParentId(id);
                entity.setName(dl.getProjectName()+"&&"+dl.getNum());
                entity.setOrderNum(dl.getOrderNum());
                mngEntities.add(entity);
            });
            saveOrUpdateBatch(mngEntities);
        }
    }

    @Override
    public PageInfo<ModelMngEntity> pageList(String name,Integer pageNum,Integer pageSize) {
        LambdaQueryWrapper<ModelMngEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ModelMngEntity::getParentId,"-1");
        if (!StringUtils.isEmpty(name)){
            wrapper.like(ModelMngEntity::getName,name);
        }
        wrapper.orderByDesc(ModelMngEntity::getCreateTime);
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(list(wrapper));
    }

    @Override
    @Transactional
    public void changeOrder(String id, Integer status) {
        ModelMngEntity entity = getById(id);
        if (entity == null){
            throw new ServiceException("数据不存在，请核对数据是否存在！");
        }
        int i =entity.getOrderNum() ;
        if (status == 0){
            // 升
            if (i != 1){
                i =  i-1;
            }
        }else {
            List<ModelMngEntity> list = list(new LambdaQueryWrapper<ModelMngEntity>().orderByDesc(ModelMngEntity::getCreateTime));
            if (!CollectionUtils.isEmpty(list)){
                if (!Objects.equals(list.get(0).getOrderNum(), i)){
                    i =  i+1;
                }
            }
        }
        ModelMngEntity mng = getOne(new LambdaQueryWrapper<ModelMngEntity>().eq(ModelMngEntity::getOrderNum,i).eq(ModelMngEntity::getParentId,entity.getParentId()));
        if (mng != null){
            mng.setOrderNum(entity.getOrderNum());
            saveOrUpdate(mng);
        }
        entity.setOrderNum(i);
        saveOrUpdate(entity);
    }
}
